
 var version = '1.0.01';
 
 var gulp = require('gulp');
 var replace = require('gulp-replace');
 var uglify = require('gulp-uglify');
 var minifyCss = require('gulp-clean-css');
 var gulpSequence = require('gulp-sequence');
 var ngAnnotate = require('gulp-ng-annotate');
 var lightmin = require('gulp-lightmin');
 var htmlmin = require('gulp-htmlmin');
 var clean = require('gulp-clean');
 var browserSync=require( 'browser-sync');
 var inject=require('gulp-inject');
 var rename=require('gulp-rename');
 var concat=require('gulp-concat');
 /*公共文件*/
 gulp.task('libs:file',function () {
     gulp.src(['./libs/**/*'])
         .pipe(gulp.dest('dist/libs'));
 });
 gulp.task('libs:js',function(){
     gulp.src([
         'dist/libs/**/jquery.js','dist/libs/**/bootstrap.js',
         'dist/libs/**/moment.js','dist/libs/**/angular.js',
         'dist/libs/**/angular-animate.js','dist/libs/**/angular-cookies.js',
         'dist/libs/**/angular-messages.js','dist/libs/**/angular-resource.js',
         'dist/libs/**/angular-sanitize.js','dist/libs/**/angular-touch.js',
         'dist/libs/**/Chart.js','dist/libs/**/angular-chart.js',
         'dist/libs/**/toaster.js','dist/libs/**/angular-ui-router.js',
         'dist/libs/**/ngDialog.min.js','dist/libs/**/loading-bar.min.js',
         'dist/libs/**/ngStorage.js','dist/libs/**/ui-utils.js',
         'dist/libs/**/ui-bootstrap-tpls-1.3.2.js','dist/libs/**/d3.js',
         'dist/libs/**/nv.d3.js','dist/libs/**/angular-nvd3.js'
     ])
         .pipe(ngAnnotate())
         .pipe(concat('main.js'))
         .pipe(uglify())
         .pipe(gulp.dest('dist/libs'));
 });
 gulp.task('libs:css',function () {
     gulp.src([
         'dist/libs/**/ngDialog.css','dist/libs/**/ngDialog-theme-default.min.css',
         'dist/libs/**/ngDialog-theme-flat.css','dist/libs/**/ngDialog-theme-plain.min.css',
         'dist/libs/**/loading-bar.min.css','dist/libs/**/toaster.css',
         'dist/libs/**/angular-confirm.min.css','dist/libs/**/nv.d3.css'
     ])
         .pipe(concat('main.css'))
         .pipe(gulp.dest('dist/libs'));
 });
 
 /*项目文件*/
 gulp.task('build:clean', function () {
     return gulp.src(['dist/**/*.*','!dist/libs/**/*.*'], {read: false})
         .pipe(clean());
 });
 gulp.task('build:js',function () {
     gulp.src(['./src/js/**/*.js'])
         .pipe(ngAnnotate())
         .pipe(uglify())
         .pipe(gulp.dest('./dist/js'));
 });
 gulp.task('build:css',function () {
     gulp.src(['./src/css/**/*.css'])
         .pipe(minifyCss())
         .pipe(gulp.dest('./dist/css'));
 });
 gulp.task('build:tpl',function () {
     gulp.src(['./src/tpl/**/*.html'])
         .pipe(lightmin())
         .pipe(gulp.dest('./dist/tpl'));
 });
 gulp.task('build:fonts',function () {
     gulp.src(['./src/fonts/**/*'])
         .pipe(gulp.dest('./dist/fonts'));
 });
 gulp.task('build:img',function () {
     gulp.src(['./src/img/**/*'])
         .pipe(gulp.dest('./dist/img'));
 });
 gulp.task('build:index',function () {
     gulp.src(['./src/index.html'])
         .pipe(htmlmin({collapseWhitespace: true}))
         .pipe(gulp.dest('./dist/'));
 });
 
 gulp.task('build:index:api',function () {
     gulp.src(['./dist/index.html'])
         .pipe(htmlmin())
         .pipe(replace('http://testnewadmin.hellokey.com/', 'http://testnewadmin.hellokey.com/'))
         .pipe(replace(/\.\.\/libs/g, 'libs'))
         .pipe(replace(/"js\/(.*?)\.js/g,'"js/$1.js?'+version+'_'+Date.parse(new Date())))
         .pipe(gulp.dest('./dist/'));
 });
 
 gulp.task('build:1',['build:clean'],gulpSequence('build:js','build:css','build:tpl','build:fonts','build:img','build:index'));
 gulp.task('build:2',gulpSequence('build:index:api','libs:file','libs:js','libs:css'));


/*-----------------------------静态服务---------------------------------*/
gulp.task("watch",["index"],function (){
    gulp.watch(['src/**/*']).on('change',browserSync.reload);
    gulp.watch(['src/tpl.html'],["index"]);
});
gulp.task("serve", ["watch"],function (){
    browserSync({
        server: {
            baseDir: './src',
            routes: {
                "/libs": "libs"
            }
        }
    });
});
gulp.task("serve:dist", function (){
    browserSync({
        server: {
            baseDir: './dist',
            routes: {
                "/libs": "libs"
            }
        }
    });
});
gulp.task('index', function () {
    var target = gulp.src('src/tpl.html');
    var pilugins = gulp.src([
        'src/js/services/**/*.js',
        'src/js/filters/**/*.js',
        'src/js/directives/**/*.js',
        '!src/js/directives/app-contenteditable.js',
        '!src/js/directives/editor.js',
        '!src/js/directives/order-status.js',
        '!src/js/directives/pagebar.js',
        '!src/js/directives/ui-btn-loading.js',
    ], {read: false});

    return target.pipe(inject(pilugins,{
        ignorePath:'src/',
        addRootSlash: false,
        removeTags: true,}))
        .pipe(rename("index.html"))
        .pipe(gulp.dest('src/'));
});
gulp.task('minhtml',function () {
    gulp.src(['./demo.html'])
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest('./'));
});